/*LICENSE_BEGIN
 * ******************************************************************
 * All original parts of SoccerScope2003 have copyright (2003) by
 * Team YowAI, represented by Mr. Shuhei Shiota
 * The Department of Creative Informatics
 * The Graduate School of Information Science and Technology
 * The University of Tokyo
 * 
 * Additional classes (except libraries in /lib) were developed by 
 * the Dainamite Robocup-Team of the DAI-Labor, TU-Berlin 
 * (www.dai-labor.de)
 * 
 * Extended/new classes/packages are:
 *   soccerscope.analyzer.LogfileAnalyzer
 *   soccerscope.daistatistic.*
 *   soccerscope.daistatistic.charts.*
 *   soccerscope.daistatistic.tools.*
 *   soccerscope.db.*
 *   soccerscope.dialog.*
 *   soccerscope.model.PlayerWorldModel
 *   soccerscope.model.SimplePlayer
 *   soccerscope.net.AgentConnector
 *   soccerscope.training.*
 *   soccerscope.training.conditions.*
 * ******************************************************************
LICENSE_END*/
package soccerscope.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import javax.swing.AbstractButton;

public class ButtonManager {

	private static HashMap<String, ArrayList<AbstractButton>> map = new HashMap<String, ArrayList<AbstractButton>>();

	private ButtonManager() {
	};

	public static void addButton(String group, AbstractButton button) {
		ArrayList<AbstractButton> array;
		boolean enable = true;
		boolean select = false;
		if (map.containsKey(group)) {
			array = map.get(group);
			enable = ((AbstractButton) array.get(0)).isEnabled();
			select = ((AbstractButton) array.get(0)).isSelected();
		} else {
			array = new ArrayList<AbstractButton>();
			map.put(group, array);
		}
		array.add(button);
		button.setEnabled(enable);
		button.setSelected(select);
	}

	public static void setEnabled(String group, boolean enable) {
		if (map.containsKey(group)) {
			ArrayList array = (ArrayList) map.get(group);
			Iterator it = array.iterator();
			while (it.hasNext()) {
				((AbstractButton) it.next()).setEnabled(enable);
			}
		}
	}

	public static void setSelected(String group, boolean select) {
		if (map.containsKey(group)) {
			ArrayList array = (ArrayList) map.get(group);
			Iterator it = array.iterator();
			while (it.hasNext()) {
				((AbstractButton) it.next()).setSelected(select);
			}
		}
	}

	public static void printAllKeys() {
		Iterator it = map.keySet().iterator();
		while (it.hasNext()) {
			System.out.println(it.next());
		}
	}
}
